package medusa

import (
	"sync"
	"sync/atomic"
)

type StateCounter struct {
	sync.WaitGroup

	once       sync.Once
	is_working int32
}

func (self *StateCounter) SwicthWorking() bool {
	/*
		ret := false
		self.once.Do(func() {
			self.Add(1)
			ret = true
	        atomic.StoreInt32 (&self.is_working, 1)
		})

		return ret
	*/
	if atomic.CompareAndSwapInt32(&self.is_working, 0, 1) {
		self.Add(1)
		return true
	}
	return false
}

/*
func (self *State) SwicthWorking() {
	if !self.SwicthWorking() {
		self.Wait()
	}
}*/

func (self *StateCounter) SwicthClosed() bool {
	if atomic.CompareAndSwapInt32(&self.is_working, 1, 0) {
		self.Done()
		return true
	}
	return false
}

func (self *StateCounter) IsWorking() bool {
	return atomic.LoadInt32(&self.is_working) == 1
}

func (self *StateCounter) NotWorking() bool {
	return atomic.LoadInt32(&self.is_working) == 0
}

/////////////////////////////
/*
const (
	NOT_SET     int32 = 0
	SETTING     int32 = 1
	ALREADY_SET int32 = 2
)


type OnceSet struct {
    sync.WaitGroup

    state int32
}

func (self *OnceSet) AlreadySet() bool {
    return atomic.LoadInt32(&self.state) == ALREADY_SET
}

func (self *OnceSet) NotSet() bool {
    return atomic.LoadInt32(&self.state) != ALREADY_SET
}

func (self *OnceSet) Set(set func()) {
    if self.AlreadySet() {
        return
    }
    if atomic.CompareAndSwapInt32(&self.state, NOT_SET, SETTING) {
        self.Add(1)
        if set != nil {
            set()
        }
        self.Done()
    } else {
        self.Wait()
    }
}

func (self *OnceSet) Reset() bool {
    return atomic.CompareAndSwapInt32(&self.state, ALREADY_SET, NOT_SET)
}
*/

/*
func (self *OnceSet) RequestSet() bool {
	if self.AlreadySet() {
		return false
	}
	if atomic.CompareAndSwapInt32(&self.state, NOT_SET, SETTING) {
		self.Add(1)
		return true
	}
	self.Wait()
	return false
}

func (self *OnceSet) SetOver() bool {
	if atomic.CompareAndSwapInt32(&self.state, 1, 2) {
		self.Done()
		return true
	}
	return false
}*/

/*
//CountState

type CountState struct {
	wg    sync.WaitGroup
	state int64
}

func (self *CountState) SwicthWorking() bool {
	return atomic.CompareAndSwapInt64(&self.state, 0, 1)
}

func (self *CountState) IsWorking() bool {
	return atomic.LoadInt64(&self.state) > 0
}

func (self *CountState) NotWorking() bool {
	return atomic.LoadInt64(&self.state) == 0
}

func (self *CountState) Add() int64 {
	self.wg.Add(1)
	return atomic.AddInt64(&self.state, 1)
}

func (self *CountState) Done() int64 {
	self.wg.Done()
	return atomic.AddInt64(&self.state, -1)
}

func (self *CountState) Wait() bool {
	return atomic.LoadInt64(&self.state) == 0
}

//SwitchState

type SwitchState struct {
	wg    sync.WaitGroup
	state int64
}

type SwitchState struct {
	wg    sync.WaitGroup
	state int64
}

func (self *SwitchState) SwicthWorking() bool {
	return atomic.CompareAndSwapInt64(&self.state, 0, 1)
}

func (self *SwitchState) SwicthReset() bool {
	return atomic.CompareAndSwapInt64(&self.state, 1, 0)
}

func (self *SwitchState) IsWorking() bool {
	return atomic.LoadInt64(&self.state) > 0
}

func (self *SwitchState) NotWorking() bool {
	return atomic.LoadInt64(&self.state) == 0
}

func (self *SwitchState) Wait() bool {
	return atomic.LoadInt64(&self.state) == 0
}
*/
